<?php

/**
 * This class is in development
 *
 * @author hikui
 */
class Auth
{

    static $_instance;
    private $db_link;

    private function __construct()
    {
        
    }

    private function __clone()
    {

    }

    private function Conn()
    {
        $this->db_link = new mysqli("localhost", "root", "root", "imbaphptest"); //this is only for test
    }

    public static function GetInstance()
    {
        if (!(self::$_instance instanceof self))
        {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    public function Authenticate($auth_adapter)
    {
        $this->Conn();
        $username_field = $auth_adapter->GetUsernameField();
        $password_field = $auth_adapter->GetPasswordField();
        $username = $auth_adapter->GetUserName();
        $password = $auth_adapter->GetPassword();
        $table_name = $auth_adapter->GetTableName();

        $query = "select * from `$table_name` where `$password_field`='$password' and `$username_field`='$username'";
        $result = mysqli_query($this->db_link, $query);
        $row_cont = mysqli_num_rows($result);
        if ($row_cont == 1)
        {
            return" true";
        }
        else
        {
            return "false";
        }
    }

}

//test
require_once 'AuthAdapter.php';
$adapter = new AuthAdapter("test", "users", "username", "password");
$adapter->SetPassword("kingdom301");
$adapter->SetUserName("hikui");
$auth = Auth::GetInstance();
$result = $auth->Authenticate($adapter);
echo $result;
?>
